﻿using BLL;
using Model;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditUser : FormPage
    {
        private Sys_UserBLL userBLL = new Sys_UserBLL();
        private Sys_DepartmentBLL departmentBLL = new Sys_DepartmentBLL();
        private Sys_RoleBLL roleBLL = new Sys_RoleBLL();
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                var entity = userBLL.Find(Convert.ToDecimal(FORM_KEY));
                FORM_USER_NAME.Text = entity.USER_NAME;
                //FORM_COMPANY_ID.SelectedValue = entity.USER_COMPANY.ToString();
                //FORM_DEPARTMENT_ID.SelectedValue = entity.USER_DEPARTMENT.ToString();
                //FORM_ROLE_STRING.Text=
                //FORM_USER_NAME.Enabled = false;
                BinRole(entity.USER_ROLEID);
                BindCompany(entity.USER_COMPANY.ToString());
                BindDepartment(entity.USER_DEPARTMENT.ToString());
                FORM_SEX.SelectedValue = entity.USER_SEX;
                FORM_JOB_STATE.SelectedValue = entity.USER_JOBSTATUS;
                FORM_LOGIN_NAME.Text = entity.USER_LOGINNAME;
                FORM_LOGIN_NAME.Enabled = false;
                FORM_PHONE.Text = entity.USER_PHONE;
                FORM_LOGIN_STATE.SelectedValue = entity.USER_LOGINSTATUS;
                LAST_LOGIN_TIME.Text = entity.USER_LOGINTIME.ToString();
                USER_ONLINE_STATE.Text = entity.USER_ONLINESTATUS == "1" ? "在线" : "离线";
            }
            else
            {
                BindCompany();
                BindDepartment();
                DIV_1.Style.Add("display", "none");
                DIV_2.Style.Add("display", "none");
            }
        }

        private void BinRole(string idStr)
        {
            var text = "";
            if (idStr != "")
            {
                text = roleBLL.GetRoleNames(idStr);
            }
            FORM_ROLE_IDS.Value = idStr;
            FORM_ROLE_STRING.Text = text;
        }

        private void BindCompany(string id = null)
        {
            DataTable dtCompany = companyBLL.GetInfoByPId((int)CurrentUserCompanyID);
            FORM_COMPANY_ID.DataSource = dtCompany;
            FORM_COMPANY_ID.DataTextField = "CMY_NAME";
            FORM_COMPANY_ID.DataValueField = "CMY_ID";
            FORM_COMPANY_ID.DataBind();
            FORM_COMPANY_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
            if (id == null)
            {
                FORM_COMPANY_ID.SelectedValue = "-1";
            }
            else
            {
                FORM_COMPANY_ID.SelectedValue = id;
            }
        }

        private void BindDepartment(string id = null)
        {
            if (id == null)
            {
                FORM_DEPARTMENT_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
                FORM_DEPARTMENT_ID.SelectedValue = "-1";
            }
            else
            {
                var dt = departmentBLL.GetInfoALLCmyID(FORM_COMPANY_ID.SelectedValue);

                FORM_DEPARTMENT_ID.DataSource = dt;
                FORM_DEPARTMENT_ID.DataTextField = "DEPTNAME";
                FORM_DEPARTMENT_ID.DataValueField = "DEPTID";
                FORM_DEPARTMENT_ID.DataBind();
                FORM_DEPARTMENT_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
                if (dt.AsEnumerable().Where(a => a["DEPTID"].ToString() == id).Count() > 0)
                {
                    FORM_DEPARTMENT_ID.SelectedValue = id;
                }
                else
                {
                    FORM_DEPARTMENT_ID.SelectedValue = "-1";
                }
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_USER_NAME.Text == "")
            {
                MessageBox.Show(this, "请选择用户名称");
                return;
            }
            if (FORM_COMPANY_ID.SelectedValue == "-1")
            {
                MessageBox.Show(this, "请选择公司");
                return;
            }
            if (FORM_DEPARTMENT_ID.SelectedValue == "-1")
            {
                MessageBox.Show(this, "请选择部门");
                return;
            }
            if (FORM_ROLE_STRING.Text == "" || FORM_ROLE_IDS.Value == "")
            {
                MessageBox.Show(this, "请选择系统角色");
                return;
            }

            Sys_User entity;
            if (FORM_IFEDIT)
            {
                entity = userBLL.Find(Convert.ToDecimal(FORM_KEY));

                #region 修改

                entity.USER_COMPANY = Convert.ToDecimal(FORM_COMPANY_ID.SelectedValue);
                entity.USER_DEPARTMENT = Convert.ToDecimal(FORM_DEPARTMENT_ID.SelectedValue);
                entity.USER_ROLEID = FORM_ROLE_IDS.Value;
                entity.USER_SEX = FORM_SEX.SelectedValue;
                entity.USER_NAME = FORM_USER_NAME.Text.Trim();
                entity.USER_PHONE = FORM_PHONE.Text.Trim();
                entity.USER_JOBSTATUS = FORM_JOB_STATE.SelectedValue;
                entity.USER_LOGINSTATUS = FORM_LOGIN_STATE.SelectedValue;
                if (FORM_PASSWORD.Text.TrimEnd() != "")
                {
                    entity.USER_PWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(FORM_PASSWORD.Text.Trim(), "MD5");
                }

                if (userBLL.Update(entity, CurrentUserName))
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改失败");
                }

                #endregion 修改
            }
            else
            {
                if (userBLL.GetInfoByLogin(FORM_LOGIN_NAME.Text.Trim().ToUpper()) != null)
                {
                    MessageBox.Show(this, "登录名已存在！");
                    return;
                }
                if (FORM_LOGIN_NAME.Text.Trim() == "")
                {
                    MessageBox.Show(this, "登录名不能为空！");
                    return;
                }
                if (FORM_PASSWORD.Text.TrimEnd() == "")
                {
                    MessageBox.Show(this, "密码不能为空！");
                    return;
                }

                #region 新增

                entity = new Sys_User
                {
                    USER_NAME = FORM_USER_NAME.Text.Trim(),
                    USER_LOGINNAME = FORM_LOGIN_NAME.Text.Trim().ToUpper(),
                    USER_PWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(FORM_PASSWORD.Text.Trim(), "MD5"), //FORM_PASSWORD.Text.Trim(),
                    USER_ROLEID = FORM_ROLE_IDS.Value,
                    USER_JOBSTATUS = FORM_JOB_STATE.SelectedValue,
                    USER_SEX = FORM_SEX.SelectedValue,
                    USER_PHONE = FORM_PHONE.Text,
                    USER_LOGINSTATUS = FORM_LOGIN_STATE.SelectedValue,
                    USER_ONLINESTATUS = "0",
                    USER_LOGINTIME = DateTime.Now,
                    USER_COMPANY = Convert.ToDecimal(FORM_COMPANY_ID.SelectedValue),
                    USER_DEPARTMENT = Convert.ToDecimal(FORM_DEPARTMENT_ID.SelectedValue)
                };
                if (userBLL.Insert(entity, CurrentUserName))
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditUser.aspx?IF_EDIT=FALSE");//TRUE&ID="+entity.USER_ID.ToString());
                    //MessageBox.Show(this,"新增成功");
                }
                else
                {
                    MessageBox.Show(this, "新增失败");
                }

                #endregion 新增
            }
        }

        protected void FORM_COMPANY_CHANGE(object sender, EventArgs e)
        {
            var dt = departmentBLL.GetInfoALLCmyID(FORM_COMPANY_ID.SelectedValue);

            FORM_DEPARTMENT_ID.DataSource = dt;
            FORM_DEPARTMENT_ID.DataTextField = "DEPTNAME";
            FORM_DEPARTMENT_ID.DataValueField = "DEPTID";
            FORM_DEPARTMENT_ID.DataBind();
            FORM_DEPARTMENT_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
            FORM_DEPARTMENT_ID.SelectedValue = "-1";
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑用户\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加用户\");");
            }
        }
    }
}